using System;
using System.Linq;
using System.Text;
using DevExpress.Xpo;
using DevExpress.ExpressApp;
using System.ComponentModel;
using DevExpress.ExpressApp.DC;
using DevExpress.Data.Filtering;
using DevExpress.Persistent.Base;
using System.Collections.Generic;
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
using DevExpress.ExpressApp.SystemModule;

namespace DomainComponents.XCRM
{
    public enum ProductStatus
    {
        Inactive = 0,
        Active = 1
    }

    public class ProductValidationRules
    {
        public const string NameIsRequired = "ProductNameIsRequired";
        public const string PriceIsGreaterThanOrEqualZero = "PriceIsGreaterThanOrEqualZero";
    }

    [DomainComponent, XafDefaultProperty("Name")]
    [VisibleInReports]
    [ImageName("BO_Product")]
    [ListViewFilter("Product_ActiveProducts", "Status='Active'", "Active Products", true, Index = 0)]
    [ListViewFilter("Product_AllProducts", "", "All Products", Index = 1)]
    [ListViewFilter("Product_InactiveProducts", "Status='Inactive'", "Inactive Products", Index = 2)]
    [XafDisplayName("Product")]
    public interface IProduct
    {
        [RuleRequiredField(ProductValidationRules.NameIsRequired, DefaultContexts.Save)]
        string Name { get; set; }

        [RuleValueComparison(ProductValidationRules.PriceIsGreaterThanOrEqualZero, DefaultContexts.Save, ValueComparisonType.GreaterThanOrEqual, 0)]
        decimal Price { get; set; }

        [System.ComponentModel.DefaultValue(ProductStatus.Active)]
        ProductStatus Status { get; set; }

        ICategory Category { get; set; }

        [Action(PredefinedCategory.Edit, Caption = "Activate Product...", AutoCommit = true,
            ConfirmationMessage = "This operation will set the selected Product as Active.", TargetObjectsCriteria = "Status != 'Active'",
            SelectionDependencyType = MethodActionSelectionDependencyType.RequireMultipleObjects, ImageName = "Action_Workflow_Activate")]
        void Activate();

        [Action(PredefinedCategory.Edit, Caption = "Deactivate Product...", AutoCommit = true,
            ConfirmationMessage = ProductLogic.DeactivateConfirmationMessage, TargetObjectsCriteria = "Status = 'Active'",
            SelectionDependencyType = MethodActionSelectionDependencyType.RequireMultipleObjects, ImageName = "Action_Workflow_Deactivate")]
        void Deactivate();
    }

    [DomainLogic(typeof(IProduct))]
    public class ProductLogic
    {
        public const string DeactivateConfirmationMessage =
@"This action will set the Product as inactive. There may be records in the system that continue to reference these inactive records.

Do you want to proceed?";

        public static void Deactivate(IProduct product)
        {
            product.Status = ProductStatus.Inactive;
        }
        public static void Activate(IProduct product)
        {
            product.Status = ProductStatus.Active;
        }
    }


    //[DefaultClassOptions]
    ////[ImageName("BO_Contact")]
    ////[DefaultProperty("DisplayMemberNameForLookupEditorsOfThisType")]
    ////[DefaultListViewOptions(MasterDetailMode.ListViewOnly, false, NewItemRowPosition.None)]
    ////[Persistent("DatabaseTableName")]
    //// Specify more UI options using a declarative approach (http://documentation.devexpress.com/#Xaf/CustomDocument2701).
    //public class IProduct : BaseObject
    //{ // Inherit from a different class to provide a custom primary key, concurrency and deletion behavior, etc. (http://documentation.devexpress.com/#Xaf/CustomDocument3146).
    //    public IProduct(Session session)
    //        : base(session)
    //    {
    //    }
    //    public override void AfterConstruction()
    //    {
    //        base.AfterConstruction();
    //        // Place your initialization code here (http://documentation.devexpress.com/#Xaf/CustomDocument2834).
    //    }
    //    //private string _PersistentProperty;
    //    //[XafDisplayName("My display name"), ToolTip("My hint message")]
    //    //[ModelDefault("EditMask", "(000)-00"), Index(0), VisibleInListView(false)]
    //    //[Persistent("DatabaseColumnName"), RuleRequiredField(DefaultContexts.Save)]
    //    //public string PersistentProperty {
    //    //    get { return _PersistentProperty; }
    //    //    set { SetPropertyValue("PersistentProperty", ref _PersistentProperty, value); }
    //    //}

    //    //[Action(Caption = "My UI Action", ConfirmationMessage = "Are you sure?", ImageName = "Attention", AutoCommit = true)]
    //    //public void ActionMethod() {
    //    //    // Trigger a custom business logic for the current record in the UI (http://documentation.devexpress.com/#Xaf/CustomDocument2619).
    //    //    this.PersistentProperty = "Paid";
    //    //}
    //}
}
